using BW.WMS.Application.Features.Outbound.Dtos;
using BW.WMS.Application.Features.Outbound.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace BW.WMS.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutboundStatisticsController : ControllerBase
    {
        private readonly IOutboundStatisticsService _outboundStatisticsService;
        private readonly ILogger<OutboundStatisticsController> _logger;

        public OutboundStatisticsController(IOutboundStatisticsService outboundStatisticsService, ILogger<OutboundStatisticsController> logger)
        {
            _outboundStatisticsService = outboundStatisticsService;
            _logger = logger;
        }

        ///// <summary>
        ///// 获取出库统计数据
        ///// </summary>
        ///// <param name="request">请求参数</param>
        ///// <returns>出库统计数据</returns>
        //[HttpPost]
        //public async Task<IActionResult> GetOutboundStatistics([FromBody] OutboundPredictionRequest request)
        //{
        //    try
        //    {
        //        if (request == null)
        //        {
        //            return BadRequest("请求参数不能为空");
        //        }

        //        if (request.StartDate > request.EndDate)
        //        {
        //            return BadRequest("开始日期不能大于结束日期");
        //        }

        //        // 限制查询范围，避免查询过大数据量
        //        if ((request.EndDate - request.StartDate).TotalDays > 365 * 2)
        //        {
        //            return BadRequest("查询时间范围不能超过两年");
        //        }

        //        var result = await _outboundStatisticsService.GetOutboundStatisticsAsync(request.StartDate, request.EndDate);
        //        return Ok(result);
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError(ex, "获取出库统计数据时发生错误");
        //        return StatusCode(500, "获取出库统计数据时发生错误: " + ex.Message);
        //    }
        //}

        /// <summary>
        /// 获取出库预测数据
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns>出库预测数据</returns>
        [HttpPost]
        public async Task<IActionResult> GetOutboundPrediction([FromBody] OutboundPredictionRequest request)
        {
            try
            {
                if (request == null)
                {
                    return BadRequest("请求参数不能为空");
                }

                if (request.StartDate > request.EndDate)
                {
                    return BadRequest("开始日期不能大于结束日期");
                }

                // 限制预测范围为最多一年
                if ((request.EndDate - request.StartDate).TotalDays > 365)
                {
                    return BadRequest("预测时间范围不能超过一年");
                }

                // 如果开始日期是过去的日期，调整为当前日期
                if (request.StartDate < DateTime.Today)
                {
                    _logger.LogInformation($"调整预测开始日期从 {request.StartDate:yyyy-MM-dd} 到 {DateTime.Today:yyyy-MM-dd}");
                    request.StartDate = DateTime.Today;
                }

                var result = await _outboundStatisticsService.GetOutboundPredictionAsync(request.StartDate, request.EndDate);
                
            

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取出库预测数据时发生错误");
                return StatusCode(500, "获取出库预测数据时发生错误: " + ex.Message);
            }
        }
    }
} 